#include <config.h>
#include <utils.h>
#include <os.h>
#include <device.h>
#include <printk.h>
#include <syserr.h>
#include "smart_sensor.h"
#include "filter.h"
#include "sensor_test.h"

static device_t *dev_infrared,*dev_visiable,*dev_ntc,*dev_veml6030;
static int ntc_value,infrared_value,visible_value,lux_value;
static struct es_filter infrared_filter,visible_filter,lux_filter;

int get_ir_value(void)
{
	return infrared_value;
}
	
int get_vl_value(void)
{
	return visible_value;
}

int get_ntc_value(void)
{
	return ntc_value;
}

int get_lux_value(void)
{
    return lux_value;
}

void setup_ir(void)
{
	dev_infrared = device_find("ir");
    assert(dev_infrared);
    device_open(dev_infrared, 0);
}

void setup_vl(void)
{
	dev_visiable = device_find("vl");
    assert(dev_visiable);
    device_open(dev_visiable, 0);
}

void setup_ntc(void)
{
	dev_ntc = device_find("ntc");
	assert(dev_ntc);
	device_open(dev_ntc,0);
}

void setup_veml6030(void)
{
    dev_veml6030 = device_find("veml6030");
    assert(dev_veml6030);
    device_open(dev_veml6030, 0);
}

void smart_sensor_read_hook_1s(void)
{
	static int hook_times = 0;
	
	ntc_value = device_read(dev_ntc,0,NULL,0);
	infrared_value = device_read(dev_infrared,0,NULL,0);
	visible_value = device_read(dev_visiable,0,NULL,0);
	lux_value = device_read(dev_veml6030,NULL,0,0);

	if(hook_times == 0)
	{
		es_filter_init(&lux_filter,(float)lux_value);
		es_filter_init(&visible_filter,(float)visible_value);
		es_filter_init(&infrared_filter,(float)infrared_value);
		hook_times = 1;
	}
	
	lux_value 		= (int)es_filter_output(&lux_filter,(float)lux_value);
	visible_value   = (int)es_filter_output(&visible_filter,(float)visible_value);
	infrared_value  = (int)es_filter_output(&infrared_filter,(float)infrared_value);
	
	SENSOR_DEBUG(lux_value,visible_value,infrared_value,ntc_value);
}

